﻿using HookahProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace HookahProject.Controllers
{
    public class TobaccoController : Controller
    {
        private HookahEntities2 _wn = new HookahEntities2();

        private IQueryable<Tobacco> GetTobaccos(string UserID)
        {
            return _wn.Tobacco.Where(it => it.UserId == UserID);
        }

        private Tobacco GetTobacco(int id)
        {
            return _wn.Tobacco.SingleOrDefault(it => it.Id == id);
        }

        private void AddTobacco(string Brand, string Flavor, float Price, float Rating, string Comments, string IsExists, string UserId)
        {
            var newTobacco = new Tobacco { Brand = Brand, Flavor = Flavor, Price = Price, Rating = Rating, Comments = Comments, IsExists = IsExists, UserId = UserId };
            _wn.Tobacco.AddObject(newTobacco);
            _wn.SaveChanges();
        }

        private void EditTobacco(Tobacco obj)
        {
            Tobacco old = GetTobacco(obj.Id);
            old.Brand = obj.Brand;
            old.Flavor = obj.Flavor;
            old.Price = obj.Price;
            old.Rating = obj.Rating;
            old.Comments = obj.Comments;
            old.IsExists = obj.IsExists;
            _wn.SaveChanges();

        }

        private void DeleteTobacco(Tobacco obj)
        {
            Tobacco del = GetTobacco(obj.Id);
            _wn.DeleteObject(del);
            _wn.SaveChanges();
        }

        public ActionResult Index()
        {
            MembershipUser myObject = Membership.GetUser();
            string UserID = myObject.ProviderUserKey.ToString();
            return View(GetTobaccos(UserID));
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            return View(GetTobacco(id));
        }

        [HttpPost]
        public ActionResult Edit(Tobacco obj)
        {
            if (ModelState.IsValid)
            {
                EditTobacco(obj);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        public ActionResult Delete(Tobacco obj)
        {
            DeleteTobacco(obj);
            return RedirectToAction("Index");
        }

        [HttpGet]
        public ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Add(string Brand, string Flavor, float Price, float Rating, string Comments, string IsExists, string UserId)
        {
            if (ModelState.IsValid)
            {
                MembershipUser myObject = Membership.GetUser();
                string UserID = myObject.ProviderUserKey.ToString();
                AddTobacco(Brand, Flavor, Price, Rating, Comments, IsExists, UserID);
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }

    }
}
